package com.ants.listener;

import com.ants.frame.JFileChooserFrame;
import com.ants.utils.JDBCUtils;
import org.apache.commons.lang3.StringUtils;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author 蚂蚁会花呗
 * @date 2021/11/2 11:41
 */
public class ExportBtnListener {

    public ExportBtnListener() {

    }

    public ExportBtnListener(JTextField jtf1,JTextField jtf2,JPasswordField jtf3,JButton jb1,JButton jb2){
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (StringUtils.isBlank(jtf1.getText())){
                    JOptionPane.showMessageDialog(jtf1,"请输入正确的地址jdbc:mysql://ip:port/database?characterEncoding=UTF-8");
                }else if (StringUtils.isBlank(jtf2.getText())){
                    JOptionPane.showMessageDialog(jtf1,"账号不能为空！");
                }else if (StringUtils.isBlank(jtf3.getText())){
                    JOptionPane.showMessageDialog(jtf1,"密码不能为空！");
                }else{
                    boolean flag = JDBCUtils.connectionStatus(jtf1.getText(),jtf2.getText(),jtf3.getText());
                    if (flag){
                        JOptionPane.showMessageDialog(jb1,"连接成功！");
                    }else{
                        JOptionPane.showMessageDialog(jb1,"连接失败！");
                    }
                }
            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (StringUtils.isBlank(jtf1.getText())){
                    JOptionPane.showMessageDialog(jtf1,"请输入正确的地址url！");
                }else if (StringUtils.isBlank(jtf2.getText())){
                    JOptionPane.showMessageDialog(jtf1,"账号不能为空！");
                }else if (StringUtils.isBlank(jtf3.getText())){
                    JOptionPane.showMessageDialog(jtf1,"密码不能为空！");
                }else{
                    boolean flag = JDBCUtils.connectionStatus(jtf1.getText(),jtf2.getText(),jtf3.getText());
                    if (flag){
                        //监听
                        new JFileChooserFrame(jtf1,jtf2,jtf3);
                    }else{
                        JOptionPane.showMessageDialog(jb1,"连接失败！");
                    }
                }
            }
        });
    }

}

